#!/bin/bash
clear
if [[ "$EUID" -ne 0 ]]; then
  echo -e "\033[1;31m Anda tiada kebenaran untuk menjalankan skrip ini! \033[0m"
  exit 1
fi

apt-get -qq update && apt-get -y upgrade
dependencies=('coreutils' 'curl' 'git' 'libnss3' 'socat' 'unzip' 'xz-utils')
for dep in "${dependencies[@]}"; do
  apt-get install -y $dep
done
wget -q -O - https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh | bash

if [[ ! -d /usr/local/etc/v2ray/clients ]]; then
  mkdir /usr/local/etc/v2ray/clients
fi

if [[ -f /etc/letsencrypt/live/vpn.aidan.my/fullchain.pem ]]; then
  cp /etc/letsencrypt/live/vpn.aidan.my/fullchain.pem /usr/local/etc/v2ray/v2ray.crt
fi
if [[ -f /etc/letsencrypt/live/vpn.aidan.my/privkey.pem ]]; then
  cp /etc/letsencrypt/live/vpn.aidan.my/privkey.pem /usr/local/etc/v2ray/v2ray.key
fi
chmod 644 /usr/local/etc/v2ray/v2ray.crt
chmod 644 /usr/local/etc/v2ray/v2ray.key

ipAddr=$(wget -qO- ipv4.icanhazip.com)
hostAddr=$(cat /usr/local/.environment | grep 'DOMAIN' | cut -d '=' -f 2 | head -n 1)
emailAddr=$(cat /usr/local/.environment | grep 'EMAIL' | cut -d '=' -f 2)
v2rayCrt='/usr/local/etc/v2ray/v2ray.crt'
v2rayKey='/usr/local/etc/v2ray/v2ray.key'
clientUID='f7a9a565-80df-4c59-963a-f7d6d6735289'

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": 443, # 10070
            \"protocol\": \"trojan\",
            \"settings\": {
                \"clients\": [
                    {
                        ### BEGIN doctype 2022-01-08
                        \"password\": \"$clientUID\",
                        \"email\": \"$emailAddr\"
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"alpn\": [
                        \"http/1.1\"
                    ],
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\"
        }
    ]
}" >/usr/local/etc/v2ray/trojan-tcp.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": 443,
            \"protocol\": \"vless\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"$clientUID\",
                        \"level\": 0,
                        \"email\": \"$hostAddr\"
                    }
                ],
                \"decryption\": \"none\",
                \"fallbacks\": [
                    {
                        \"dest\": 8001
                    },
                    {
                        \"alpn\": \"h2\",
                        \"dest\": 8002
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"serverName\": \"$hostAddr\",
                    \"alpn\": [
                        \"h2\",
                        \"http/1.1\"
                    ],
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        }
    ]
}" >/usr/local/etc/v2ray/vless-tcp.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"inbounds\": [
        {
            \"port\": 443,
            \"protocol\": \"vless\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"$clientUID\",
                        \"level\": 0,
                        \"email\": \"$hostAddr\"
                    }
                ],
                \"decryption\": \"none\",
                \"fallbacks\": [
                    {
                        \"dest\": 80
                    },
                    {
                        \"path\": \"/websocket\",
                        \"dest\": 1234,
                        \"xver\": 1
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"alpn\": [
                        \"http/1.1\"
                    ],
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        },
        {
            \"port\": 1234,
            \"listen\": \"127.0.0.1\",
            \"protocol\": \"vless\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"$clientUID\",
                        \"level\": 0,
                        \"email\": \"$emailAddr\"
                    }
                ],
                \"decryption\": \"none\"
            },
            \"streamSettings\": {
                \"network\": \"ws\",
                \"security\": \"none\",
                \"wsSettings\": {
                    \"acceptProxyProtocol\": true,
                    \"path\": \"/websocket\"
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\"
        }
    ]
}" >/usr/local/etc/v2ray/vless-ws.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"routing\": {
        \"domainStrategy\": \"AsIs\",
        \"rules\": [
            {
                \"type\": \"field\",
                \"ip\": [
                    \"geoip:private\"
                ],
                \"outboundTag\": \"block\"
            }
        ]
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": 443,
            \"protocol\": \"vmess\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"$clientUID\"
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        },
        {
            \"protocol\": \"blackhole\",
            \"tag\": \"block\"
        }
    ]
}" >/usr/local/etc/v2ray/vmess-tcp.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"routing\": {
        \"domainStrategy\": \"AsIs\",
        \"rules\": [
            {
                \"type\": \"field\",
                \"ip\": [
                    \"geoip:private\"
                ],
                \"outboundTag\": \"block\"
            }
        ]
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": 443,
            \"protocol\": \"vmess\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"$clientUID\"
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"ws\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        },
        {
            \"protocol\": \"blackhole\",
            \"tag\": \"block\"
        }
    ]
}" >/usr/local/etc/v2ray/vmess-ws.json

systemctl enable v2ray@trojan-tcp
systemctl start v2ray@trojan-tcp
systemctl enable v2ray@vless-tcp
systemctl start v2ray@vless-tcp
systemctl enable v2ray@vless-ws
systemctl start v2ray@vless-ws
systemctl enable v2ray@vmess-tcp
systemctl start v2ray@vmess-tcp
systemctl enable v2ray@vmess-ws
systemctl start v2ray@vmess-ws

echo
echo -e "\033[1;32m Pemasangan v2ray pakej telah selesai. \033[0m"
echo
